import {
    Component,
    OnInit,
    ViewChild,
    Injector,
    AfterViewInit,
} from "@angular/core";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import { SelectItem } from "primeng/api";
import { AppComponentBase } from "@shared/common/app-component-base";
import { Table } from "primeng/table";
import { Paginator, LazyLoadEvent } from "primeng/primeng";
import {
    WorkingHourServiceProxy,
    EmployeeSalaryServiceProxy,
    EmployeeSalaryDto,
} from "@shared/service-proxies/service-proxies";
//import { ExcelModalComponent } from '@app/shared/common/excel-modal/excel-modal.component';
import * as moment from "moment";
import { BsDatepickerViewMode } from "ngx-bootstrap/datepicker/models";
import { BsDatepickerConfig, BsDaterangepickerConfig } from "ngx-bootstrap";
import { finalize } from "rxjs/operators";
import * as G2 from "@antv/g2";
import { FileUploadModule } from "primeng/fileupload";
import { FileUpload } from "primeng/fileupload";
import { HttpClient } from "@angular/common/http";
import { AppConsts } from "@shared/AppConsts";
import { FileDownloadService } from "@shared/utils/file-download.service";

@Component({
    selector: "app-salary-manage",
    templateUrl: "./salary-manage.component.html",
    styleUrls: ["./salary-manage.component.less"],
    animations: [appModuleAnimation()],
})
export class SalaryManageComponent extends AppComponentBase
    implements AfterViewInit {
    @ViewChild("ExcelFileUpload") excelFileUpload: FileUpload;

    ngAfterViewInit(): void {}

    @ViewChild("dataTable") dataTable: Table;
    salary: EmployeeSalaryDto[];
    amount = 0;
    title = "app";
    data = {};
    chart;
    graph;
    uploadUrl: string;
    importMessage: string;
    importSuccess: string;
    showMessage: boolean;

    constructor(
        injector: Injector,
        private _employeeSalaryServiceProxy: EmployeeSalaryServiceProxy,
        private _httpClient: HttpClient,
        private _fileDownloadService: FileDownloadService
    ) {
        super(injector);
        this.uploadUrl =
            AppConsts.remoteServiceBaseUrl + "/Users/ImportSalaryExcel";
        this.showMessage = true;
    }

    uploadExcel(data: { files: File }): void {
        this.showMessage = true;
        this.salary = null;
        const formData: FormData = new FormData();
        const file = data.files[0];
        formData.append("file", file, file.name);
        this.showMessage = false;
        console.log(formData);
        this._httpClient
            .post<any>(this.uploadUrl, formData)
            .pipe(finalize(() => this.excelFileUpload.clear()))
            .subscribe((response) => {
                if (response.success) {
                    this.notify.success("导入Excel成功");
                    this.salary = response.result;
                    console.log(this.salary);
                    //this.importSalaryDtoList.salaryList = this.salary;
                } else if (response.error != null) {
                    this.notify.error("导入Excel失败");
                }
            });
    }

    onUploadExcelError(): void {
        this.notify.error("上传失败");
    }

    importSalary(): void {
        this._employeeSalaryServiceProxy
            .importSalaryDtoList(this.salary)
            .pipe(
                finalize(() => this.primengTableHelper.hideLoadingIndicator())
            )
            .subscribe((result) => {
                this.notify.success("导入工资成功");
                if (result.length > 0) {
                    this.salary = result;
                    this.importSuccess = "部分导入";
                    this.importMessage = "以下内容没有导入成功，请检查。";
                    this.showMessage = false;
                } else {
                    this.salary = null;
                    this.importSuccess = "全部导入成功。";
                    this.importMessage = "";
                    this.showMessage = false;
                }
            });
    }
}
